<?php
namespace App\Http\Controllers\Business\Admin\System;

use App\Models\AdminUser;
use App\Models\City;
use App\Models\RedEnvelopeSetLog;
use Auth;
use Exception;

class SystemManagement{
    //红包比例列表
    public static function redEnvelopeList() {
        $data = City::select('id','name', 'agentratio', 'hqratio', 'ratio', 'validdays')->get();
        return ['status'=>'success', 'data'=>$data];
    }
    //红包总部比例设置
    public static function updateRedEnvelopeHqRatio($cityId, $hqRatio) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $city = City::where('id', $cityId)->first();
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $agentRatio = $city->agentratio;
        $oldHqRatio = $city->hqratio;
        $validDays = $city->validdays;
        $ratio = $agentRatio + $hqRatio;
        $city->hqratio = $hqRatio;
        $city->ratio = $ratio;
        $city->save();
        //写入日志
        $content = '总部红包比率由' . $oldHqRatio . "%变成了" . $hqRatio ."%";
        self::writeRedEnvelopeSetLog($cityId, $agentRatio, $hqRatio, $ratio, $validDays, $content);
        return ['status'=>'success', 'message'=>'修改成功'];
    }
    //红包比例获取
    public static function getRedEnvelope() {

    }
    //修改红包比例写日志
    public static function writeRedEnvelopeSetLog($cityId, $agentRatio, $hqRatio, $ratio, $validDays, $content='') {
        if(!$cityId||($agentRatio+$hqRatio != $ratio)) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $redEnvelopeSetLog = RedEnvelopeSetLog::create([
            'cityid' => $cityId,
            'agentratio' => $agentRatio,
            'hqratio' => $hqRatio,
            'ratio' => $ratio,
            'validdays' => $validDays,
            'content' => $content,
            'adminid' => Auth::guard('adminweb')->id()
        ]);
        $redEnvelopeSetLog->save();
        return ['status'=>'success', 'message'=>'新增成功'];
    }
    //红包设置记录列表
    public static function redEnvelopeLogList($search='') {
        if($search) {
            $lists = RedEnvelopeSetLog::where('red_envelope_set_logs.created_at', '>=', '2016-07-01')
                ->join('admin_users', 'red_envelope_set_logs.adminid', '=', 'admin_users.id')
                ->join('city', 'red_envelope_set_logs.cityid', '=', 'city.id')
                ->where('city.name', 'like', '%'.$search.'%')
                ->select('red_envelope_set_logs.*', 'admin_users.realname as adminname', 'city.name as cityname')
                ->orderBy('red_envelope_set_logs.created_at', 'desc')->get();
        } else {
            $lists = RedEnvelopeSetLog::where('red_envelope_set_logs.created_at', '>=', '2016-07-01')
                ->join('admin_users', 'red_envelope_set_logs.adminid', '=', 'admin_users.id')
                ->join('city', 'red_envelope_set_logs.cityid', '=', 'city.id')
                ->select('red_envelope_set_logs.*', 'admin_users.realname as adminname', 'city.name as cityname')
                ->orderBy('red_envelope_set_logs.created_at', 'desc')->get();
        }
        return ['status'=>'success', 'data'=>$lists];
    }
}